package cn.tlb.gl.game.rebuild3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import cn.tlb.xutilsdownload.R;

public class GameModelActivity3 extends Activity {
    private ListView listView;
    private static List<Integer> ids;

    static {
        ids = new ArrayList<Integer>();
        Field[] fields = R.drawable.class.getFields();
        for (Field field : fields) {
            if (field.getName().contains("skin")) {
                try {
                    ids.add(field.getInt(null));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_game_model3);
        listView = (ListView) findViewById(R.id.skin);
        final MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(GameModelActivity3.this, GameActivity.class);
                intent.putExtra("id", adapter.getItem(position));
                startActivity(intent);
            }
        });
        listView.setClickable(true);
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return ids.size();
        }

        @Override
        public Integer getItem(int position) {
            return ids.get(position);
        }

        @Override
        public long getItemId(int position) {
            return ids.get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LinearLayout linearLayout = new LinearLayout(GameModelActivity3.this);
                linearLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                        AbsListView.LayoutParams.WRAP_CONTENT));
                ImageView imageView = new ImageView(GameModelActivity3.this);
                imageView.setId(android.R.id.content);
                imageView.setLayoutParams(new AbsListView.LayoutParams(200, 100));
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                linearLayout.addView(imageView);
                convertView = linearLayout;
            }
            ((ImageView) convertView.findViewById(android.R.id.content)).setImageResource(ids.get(position));
            return convertView;
        }
    }
}
